# from qrdecode import qrdecode
import os

import qr.zbarQr

path = '../images'
listImgTemp = []
listErrImg = []
qrList = []


def dealQrResult(item):
    qrList.append(len(item))
    r = item.get('raw').decode()
    print(r)
    if r is None:
        listErrImg.append(r)
    elif r not in listImgTemp:
        listImgTemp.append(r)
    else:
        print('重复元素')


def get_filelist(dir):
    Filelist = []

    for home, dirs, files in os.walk(path):

        for filename in files:
            # 文件名列表，包含完整路径

            Filelist.append(os.path.join(home, filename))

            # # 文件名列表，只包含文件名

            # Filelist.append( filename)

    return Filelist


if __name__ == '__main__':

    # list = get_filelist(path)
    fileList = qr.zbarQr.decodesPng(path)
    # list = qr.zbarQr.decodesJpg(path)
    for file in fileList:
        # if file.endswith('.exe'):
        #     continue
        # r = qrdecode(file)
        # ori = qr.zbarQr.decode(imgfile=file)
        # r = ori[0].get('raw').decode()
        # print(file)
        try:
            if isinstance(file, list):
                for item in file:
                    dealQrResult(item)
            else:
                dealQrResult(file)
        except:
            listErrImg.append('err')
            pass
    print('总计：%d,重复：%d,错误：%d' % (len(qrList), len(qrList) - len(listImgTemp) - len(listErrImg), len(listErrImg)))
    os.system("pause")
